/*
 *  c_board_array.c
 *  Maciago
 *
 *  Created by mmarcos on Sun Mar 31 2002.
 *  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
 *
 */

#import "board_array.h"

struct board_array *board_array_create ()
{
    struct board_array *new_board_array = malloc(sizeof(struct board_array));
    new_board_array->first = NULL;
    return new_board_array;
}

void board_array_dispose (struct board_array *array)
{
    board_array_clear (array);
    free (array);
}

void board_array_add (struct board_array *v, struct board_data *data)
{
    struct board_data *temp = board_array_last_item (v);
    if (temp != NULL) 
        temp->next = data;
    else
        v->first = data;
    data->next = NULL;
}

int board_array_count (struct board_array *v)
{
    int i = 0;
    struct board_data *current;
    
    if (v != NULL)
    {
        current = v->first;
        if (current != NULL)
        {
            for (i = 1; current->next != NULL; ++i)
                current = current->next;
        }
    }
    
    return i;
}

void board_array_clear (struct board_array *v)
{
    int i;
    struct board_data *current;
    struct board_data *temp;

    if (v != NULL)
    {
        current = v->first;
        for (i = 0; current != NULL; ++i)
        {
            temp = current;
            current = current->next;
            free (temp);
        }
        v->first = NULL;
    }
}

struct board_data *board_array_last_item (struct board_array *v)
{
    struct board_data *current = NULL;
    
    if (v != NULL)
    {
        current = v->first;
        if (current != NULL)
        {
            while (current->next != NULL)
                current = current->next;
        }
    }
    
    return current;
}

struct board_data *board_array_index (struct board_array *v, int pos)
{
    int i;
    struct board_data *current = NULL;
    
    if (v != NULL)
    {
        current = v->first;
        for (i = 0; current != NULL && i < pos; ++i)
            current = current->next;
    }
    
    return current;
}